import java.util.PriorityQueue;

class MedianFinder {
    PriorityQueue<Integer> max, min;

    public MedianFinder() {
        max = new PriorityQueue<>((a, b) -> (b - a));
        min = new PriorityQueue<>();
    }

    public void addNum(int num) {
        if (min.isEmpty() || num >= min.peek()) {
            min.offer(num);
            if (min.size() > max.size() + 1) {
                max.offer(min.poll());
            }
        } else {
            max.offer(num);
            if (max.size() > min.size()) {
                min.offer(max.poll());
            }
        }
    }

    public double findMedian() {
        if (min.size() > max.size()) {
            return min.peek();
        } else {
            return ((double) min.peek() + max.peek()) / 2;
        }
    }
}